package pers.qianyu.module.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import com.baomidou.mybatisplus.extension.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.*;
import pers.qianyu.module.core.comm.*;
import pers.qianyu.module.core.domain.system.converter.*;
import pers.qianyu.module.core.domain.system.dto.*;
import pers.qianyu.module.core.domain.system.entity.*;
import pers.qianyu.module.core.domain.system.query.*;
import pers.qianyu.module.core.domain.system.vo.*;
import pers.qianyu.module.system.dao.*;
import pers.qianyu.module.system.exception.*;
import pers.qianyu.module.system.service.*;

import java.util.*;

/**
 * @author mizzle rain
 * @date 2020-11-13 12:26
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRolePO> implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleConverter sysRoleConverter;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    @Autowired
    private SysRoleResourceDao sysRoleResourceDao;

    @Override
    public SysRoleVO queryById(Long id) {
        SysRolePO sysRolePO = sysRoleDao.selectById(id);
        if (Objects.isNull(sysRolePO)) {
            throw new SysException(SysErrorCodeEnum.INVALID_ROLE_ID);
        }
        return sysRoleConverter.convertToVO(sysRolePO);
    }

    @Override
    public void insRole(SysRoleDTO sysRoleDTO) {
        // 检测 role_name 是否存在
        QueryWrapper<SysRolePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_name", sysRoleDTO.getRoleName());
        if (sysRoleDao.selectCount(queryWrapper) > 0) {
            throw new SysException(SysErrorCodeEnum.ROLE_NAME_ALREADY_EXISTS);
        }
        // 检测 role_key 是否存在
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_key", sysRoleDTO.getRoleKey());
        if (sysRoleDao.selectCount(queryWrapper) > 0) {
            throw new SysException(SysErrorCodeEnum.ROLE_KEY_ALREADY_EXISTS);
        }
        SysRolePO sysRolePO = sysRoleConverter.convertToPO(sysRoleDTO);
        sysRoleDao.insert(sysRolePO);
    }

    @Override
    public void delById(Long id) {
        SysRolePO sysRolePO = sysRoleDao.selectById(id);
        if (Objects.isNull(sysRolePO)) {
            throw new SysException(SysErrorCodeEnum.INVALID_ROLE_ID);
        }
        sysRoleDao.deleteById(id);
        // 删除用户与角色的对应关系
        QueryWrapper<SysUserRolePO> sysUserRolePOQueryWrapper = new QueryWrapper<>();
        sysUserRolePOQueryWrapper.eq("role_id", id);
        sysUserRoleDao.delete(sysUserRolePOQueryWrapper);
        // 删除角色与权限的对应关系
        SysRoleResourcePO sysRoleResourcePO = new SysRoleResourcePO();
        sysRoleResourcePO.setRoleId(id);
        sysRoleResourceDao.delete(new QueryWrapper<>(sysRoleResourcePO));
    }

    @Override
    public void updRole(Long id, SysRoleDTO sysRoleDTO) {
        SysRolePO sysRolePO = sysRoleDao.selectById(id);
        if (Objects.isNull(sysRolePO)) {
            throw new SysException(SysErrorCodeEnum.INVALID_ROLE_ID);
        }
        String roleKey = sysRoleDTO.getRoleKey();
        SysRolePO forQuery = new SysRolePO();
        forQuery.setRoleKey(roleKey);
        Integer count = sysRoleDao.selectCount(new QueryWrapper<>(forQuery));
        if(count > 0) {
            throw new SysException(SysErrorCodeEnum.REPEATED_ROLE_KEY);
        }
        sysRolePO = sysRoleConverter.convertToPO(sysRoleDTO);
        sysRolePO.setId(id);
        sysRoleDao.updateById(sysRolePO);
    }

    @Override
    public Pagination<SysRoleVO> queryRole(SysRoleQuery sysRoleQuery) {
        Page<SysRolePO> page = new Page<>(sysRoleQuery.getPage(), sysRoleQuery.getLimit());
        SysRolePO sysRolePO = new SysRolePO();
        if (Objects.nonNull(sysRoleQuery.getPage())) {
            sysRolePO.setStatus(sysRoleQuery.getStatus());
        }
        QueryWrapper<SysRolePO> wrapper = new QueryWrapper<>(sysRolePO);
        Page<SysRolePO> rolePOPage = sysRoleDao.selectPage(page, wrapper);
        return sysRoleConverter.convertToPagination(rolePOPage);
    }
}
